<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace Think\Log\Driver;
/**
 * Class File
 *
 * @package Think\Log\Driver
 */
class File
{
    /**
     * config vars
     *
     * @var array
     */
    protected $config = [
        'log_time_format' => ' c ',
        'log_file_size' => 2097152,
        'log_path' => LOG_PATH,
    ];

    /**
     * construct
     *
     * @param array $config config
     *
     * @return File
     */
    public function __construct($config = [])
    {
        $this->config = array_merge($this->config, $config);

        if (empty($this->config['log_path'])) {
            $this->config['log_path'] = LOG_PATH;
        }
    }

    /**
     * 日志写入接口
     *
     * @access public
     *
     * @param string $log         日志信息
     * @param string $destination 写入目标
     *
     * @return void
     */
    public function write($log, $destination = '')
    {
        $now = date($this->config['log_time_format']);

        if (empty($destination)) {
            $destination = $this->config['log_path'] . date('y_m_d') . '.log';
        }

        if (!is_dir(dirname($destination))) {
            mkdir(dirname($destination), 0777, true);
        }

        //检测日志文件大小，超过配置大小则备份日志文件重新生成
        if (is_file($destination)
            && floor($this->config['log_file_size']) <= filesize($destination)
        ) {
            rename($destination, dirname($destination) . '/' . time() . '-' . basename($destination));
        }

        $msg = "[{$now}] ";

        if (!IS_CLI) {
            $msg.=$_SERVER['REMOTE_ADDR'] . ' ' . $_SERVER['REQUEST_URI'] ;
        }

        $msg .= "\n{$log}\n";

        error_log($msg, 3, $destination);
    }
}
